package org.aersoft.jokebook.activities;

import java.util.ArrayList;

import org.aersoft.jokebook.R;
import org.aersoft.jokebook.adapters.ItemFragmentAdapter;
import org.aersoft.jokebook.beans.ItemBean;
import org.aersoft.jokebook.core.Constants;
import org.aersoft.jokebook.dao.AppDAO;
import org.aersoft.jokebook.dao.api.IAppDAO;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

/**
 * Activity class to list an unique item by your respective category.
 * @author Danilo de Godoy Carolino
 */
public class ItemActivity extends FragmentActivity {

	private ViewPager mPager;
	private PagerAdapter mPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item);

		IAppDAO appDao = new AppDAO().getContext(this);
		ArrayList<ItemBean> bean = appDao.getJokeListByCategory(getIntent().getExtras().getInt(Constants.CATEGORY));
		appDao.close();

		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new ItemFragmentAdapter(getFragmentManager(), bean);
		mPager.setAdapter(mPagerAdapter);
		mPager.setCurrentItem(getIntent().getExtras().getInt(Constants.ID));
	}

	public void changeItem(View view) {
		switch (view.getId()) {
		case R.id.prev:
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
			break;
		case R.id.next:
			mPager.setCurrentItem(mPager.getCurrentItem() + 1);
			break;
		default:
			mPager.setCurrentItem(mPager.getCurrentItem() + 1);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_previous:
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
			return true;
		case R.id.action_next:
			mPager.setCurrentItem(mPager.getCurrentItem() + 1);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
